/*
 * @Author: song
 * @Description:Unity Player 保存数据
 * @Date: 2024年04月14日 星期日 17:04:31
 * @Modify:
 */


namespace Function.Preferences
{
    public static class PlayerPref
    {
        private static readonly string USER_PLAYERPREF_KEY = "SCORE_USER_SPLAYERPREF_KEY_{0}_{1}";
        public static string CurrentUsername { get; set; } = string.Empty;

        private static IPreferences _preferences;

        public static void Init(string path = null)
        {
            if (path != null)
            {
                _preferences = new BinaryPreferences(path);
            }
            else
            {
                _preferences = new BinaryPreferences();
            }
        }

        private static string GetUserKey(string key)
        {
            return string.Format(USER_PLAYERPREF_KEY, CurrentUsername, key);
        }

        public static string GetString(string key, string defaultValue, bool isPublic = false)
        {
            key = isPublic ? key : GetUserKey(key);
            return _preferences.GetString(key, defaultValue) ?? string.Empty;
        }

        public static void SetString(string key, string value, bool isPublic = false)
        {
            key = isPublic ? key : GetUserKey(key);
            _preferences.SetString(key, value);
            _preferences.Save();
        }

        public static int GetInt(string key, int defaultValue, bool isPublic = false)
        {
            key = isPublic ? key : GetUserKey(key);
            return _preferences.GetInt(key, defaultValue);
        }

        public static void SetInt(string key, int value, bool isPublic = false)
        {
            key = isPublic ? key : GetUserKey(key);
            _preferences.SetInt(key, value);
            _preferences.Save();
        }

        public static long GetLong(string key, long defaultValue, bool isPublic = false)
        {
            key = isPublic ? key : GetUserKey(key);
            return _preferences.GetLong(key, defaultValue);
        }

        public static void SetLong(string key, long value, bool isPublic = false)
        {
            key = isPublic ? key : GetUserKey(key);
            _preferences.SetLong(key, value);
            _preferences.Save();
        }

        public static float GetFloat(string key, float defaultValue, bool isPublic = false, float value = 0f)
        {
            key = isPublic ? key : GetUserKey(key);
            return _preferences.GetFloat(key, defaultValue);
        }

        public static void SetFloat(string key, float value, bool isPublic = false)
        {
            key = isPublic ? key : GetUserKey(key);
            _preferences.SetFloat(key, value);
            _preferences.Save();
        }

        public static bool GetBool(string key, bool defaultValue, bool isPublic = false)
        {
            key = isPublic ? key : GetUserKey(key);
            int dValue = defaultValue ? 1 : 0;
            return _preferences.GetInt(key, dValue) == 1;
        }

        public static void SetBool(string key, bool value, bool isPublic = false)
        {
            key = isPublic ? key : GetUserKey(key);
            int dValue = value ? 1 : 0;
            _preferences.SetFloat(key, dValue);
            _preferences.Save();
        }

        public static void DeleteAll()
        {
            _preferences.DeleteAll();
        }

        public static void DeleteKey(string key, bool isPublic = false)
        {
            key = isPublic ? key : GetUserKey(key);
            _preferences.DeleteKey(key);
        }
    }
}